package com.qinge.blog.security.filter;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


/**
 * @author QDW
 * @date 2022/5/5 21:20
 * @description 网关全局变量，允许跨域
 **/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilterImpl extends CorsFilter {
    public CorsFilterImpl() {
        super(configurationSource());
    }

    private static UrlBasedCorsConfigurationSource configurationSource() {
        UrlBasedCorsConfigurationSource source = new org.springframework.web.cors.UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        config.setAllowCredentials(true);
        // 允许所有域名进行跨域调用
        config.addAllowedOriginPattern(CorsConfiguration.ALL);
        // 放行全部原始头信息
        config.addAllowedHeader(CorsConfiguration.ALL);
        // 允许所有请求方法跨域调用
        config.addAllowedMethod(CorsConfiguration.ALL);

        source.registerCorsConfiguration("/**", config);

        return source;
    }
}
